package com.reviewit.dao.mongodb;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.MongoClient;

public class MongoUtil {
	private static MongoClient mongo = null;
	private static Morphia morphia = null;
	private static Datastore db = null;
	private static String host = "localhost";
	private static String dbName = "reviewit";

	public static MongoClient getMongo() {
		if (mongo == null)
			try {
				mongo = new MongoClient(host, 27017);
				return mongo;
			} catch (Exception e) {
				return null;
			}
		else
			return mongo;
	}

	public static Morphia getMorphia() {
		if (morphia == null)
			try {
				morphia = new Morphia();
				return morphia;
			} catch (Exception e) {
				return null;
			}
		else
			return morphia;
	}

	public static Datastore getDB() {
		if (db == null)
			try {
				db = morphia.createDatastore(mongo, dbName);
				return db;
			} catch (Exception e) {
				return null;
			}
		else
			return db;
	}

	public static String getDbName() {
		return dbName;
	}
}